﻿using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cashier
{
    /// <summary>
    /// MakeOrder.xaml 的交互逻辑
    /// </summary>
    public partial class MakeOrder : Page
    {
        private int table_id = -1;
        DataTable data;
        private SQLiteConnection connection;
        private void ShowOrders()
        {
            try
            {
                //获取表格
                data = new DataTable("desktable");
                data.Columns.Add(new DataColumn("table_id", typeof(string)));
                data.Columns.Add(new DataColumn("Number_of_people", typeof(string)));


                using (connection = new SQLiteConnection(CommonValue.sqliteConnectString))
                {
                    connection.Open();
                    string cmd = "select * from desktable where Number_of_people>0";
                    using (SQLiteCommand SQLiteCommand = new SQLiteCommand(cmd, connection))
                    {
                        //SQLiteDataAdapter SQLiteDataAdapter = new SQLiteDataAdapter(cmd, connection);
                        using (SQLiteDataReader reader = SQLiteCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string table_id = reader["table_id"].ToString();
                                string Number_of_people = reader["Number_of_people"].ToString();
                                data.Rows.Add(table_id, Number_of_people);
                                //if (updateTable(table_id))
                                //{
                                //    data.Rows.Add(table_id, Number_of_people);
                                //}

                            }

                            listView.DataContext = data.DefaultView;

                            connection.Close();
                        }
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("台桌查询失败：" + e.Message);
            }
        }

        private bool updateTable(string table_id)
        {
            try
            {
                
                
                string cmd = "select count(*) from oder where complete=0 and table_id = " + table_id;
                bool flag = false;

                using (SQLiteCommand SQLiteCommand = new SQLiteCommand(cmd, connection))
                {
                    //SQLiteCommand.ExecuteScalar();
                    //Console.WriteLine(SQLiteCommand.ExecuteScalar());
                    Object value =  SQLiteCommand.ExecuteScalar();
                    if (value!=null&& (Convert.ToInt32(value)) == 0)
                    {
                        flag = true;

                    }


                        
                }
                    

                if (flag == true) {
                    string updateCmd = "update desktable set number_of_people=0 where table_id=" + table_id;
                    using (SQLiteCommand command = new SQLiteCommand(updateCmd, connection))
                    {
                        command.ExecuteNonQuery();
                        return false;
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("台桌查询失败：" + e.Message);
            }
            return true;
        }

        public MakeOrder()
        {
            InitializeComponent();
            ShowOrders();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            //ShowOrders();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            WaitMenuWindow waitMenuWindow = new WaitMenuWindow();
            waitMenuWindow.Table_id = table_id;
            waitMenuWindow.Show();
        }

        private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            table_id = Convert.ToInt32(data.Rows[listView.SelectedIndex].ItemArray[0]);

        }
    }
}
